package com.yanfan.maintain.service.impl;

import com.yanfan.maintain.util.DateUtils;
import com.yanfan.common.exception.base.BaseException;
import com.yanfan.maintain.domain.MaintainTasks;
import com.yanfan.maintain.domain.MaintainTasksDevice;
import com.yanfan.maintain.domain.MaintainTasksDeviceParts;
import com.yanfan.maintain.domain.MaintainTopRequest;
import com.yanfan.maintain.domain.enums.MaintainTaskQueryTypeEnum;
import com.yanfan.maintain.domain.enums.TimeDimensionEnum;
import com.yanfan.maintain.mapper.MaintainTasksDao;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;

/**
 * @Author huan
 * @Description
 * @ClassName 2023年03月13日 10:47:00
 * @Version 1.0
 **/
@Service
public class MaintainGraphTable {


    @Resource
    MaintainTasksDao maintainTasksDao;
    private Map<Integer, Function<MaintainTopRequest, Map>> maintainGraphInfoType = new HashMap<>(3);

    @PostConstruct
    public void init() {
        /**
         * 任务分布
         */
        maintainGraphInfoType.put(MaintainTaskQueryTypeEnum.TASK_DISTRIBUTION.getCode(), this::taskPublish);

        /**
         * 任务详情
         */
        maintainGraphInfoType.put(MaintainTaskQueryTypeEnum.MAINTE_DETAIL.getCode(), this::maintainDetail);
        /**
         * 任务频率
         */
        maintainGraphInfoType.put(MaintainTaskQueryTypeEnum.MAINTE_PERIOD.getCode(), this::maintainPeriod);
    }


    /**
     * 任务分布
     *
     * @param maintainTopRequest
     * @return
     */
    private Map taskPublish(MaintainTopRequest maintainTopRequest) {
        Map resMap = new LinkedHashMap();
//        状态(1:待处理   2 : 处理中  3： 已完成   )
        resMap.put(1, 0);
        resMap.put(2, 0);
        resMap.put(3, 0);
        List<MaintainTasks> distributionList = maintainTasksDao.distributionList(getFlagDate(maintainTopRequest));
        if (CollectionUtils.isEmpty(distributionList)) {
            return resMap;
        }
        for (MaintainTasks maintainTasks : distributionList) {
            resMap.put(maintainTasks.getItemNum(), maintainTasks.getFlawNumber());
        }
        return resMap;
    }

    private Date getFlagDate(MaintainTopRequest maintainTopRequest) {
        Date flagDate = null;
        if (TimeDimensionEnum.DAY.getCode().equals(maintainTopRequest.getDateType())) {
            flagDate = DateUtils.getYesterdayTimestamp();
        } else if (TimeDimensionEnum.MONTH.getCode().equals(maintainTopRequest.getDateType())) {
            flagDate = DateUtils.getThisMonth();
        } else if (TimeDimensionEnum.YEAR.getCode().equals(maintainTopRequest.getDateType())) {
            flagDate = DateUtils.getThisYear();
        }
        return flagDate;
    }

    /**
     * 任务 保养详情
     */
    private Map maintainDetail(MaintainTopRequest maintainTopRequest) {
        Map<String, Integer> respMap = new LinkedHashMap();
        List<MaintainTasksDevice> devices = maintainTasksDao.maintainDevicesAndParts(getFlagDate(maintainTopRequest));
        if (CollectionUtils.isEmpty(devices)) {
            return respMap;
        }
        for (MaintainTasksDevice device : devices) {
            if (!CollectionUtils.isEmpty(device.getParts())) {
                for (MaintainTasksDeviceParts part : device.getParts()) {
                    mapIncrement(respMap, part.getPartName());
                }
            } else {
                mapIncrement(respMap, device.getDeviceName());
            }
        }
        return respMap;
    }

    /**
     * 递增
     *
     * @param resp
     * @param maintainName
     */
    private void mapIncrement(Map<String, Integer> resp, String maintainName) {
        if (resp.get(maintainName) == null) {
            resp.put(maintainName, 1);
        } else {
            resp.put(maintainName, resp.get(maintainName + 1));
        }
    }


    /**
     * 任务频率
     *
     * @param maintainTopRequest
     * @return
     */
    private Map maintainPeriod(MaintainTopRequest maintainTopRequest) {
        Map resMap = new LinkedHashMap();
        List<MaintainTasks> distributionList = maintainTasksDao.maintainPeriodList(maintainTopRequest.getDateType(), getFlagDate(maintainTopRequest));
        if (!CollectionUtils.isEmpty(distributionList)) {
            for (MaintainTasks maintainTasks : distributionList) {
                resMap.put(maintainTasks.getPlanName(), maintainTasks.getItemNum());
            }
        }
        int count = calculateKeyCount(maintainTopRequest);
        return loopCount(resMap, count);
    }

    /**
     * 计算Map的 key的个数
     *
     * @param maintainTopRequest
     * @return
     */
    private int calculateKeyCount(MaintainTopRequest maintainTopRequest) {
        int count = 0;
        // map 填充
        if (TimeDimensionEnum.DAY.getCode().equals(maintainTopRequest.getDateType())) {
            count = 24;
        } else if (TimeDimensionEnum.MONTH.getCode().equals(maintainTopRequest.getDateType())) {
            count = DateUtils.getDaysOfMonth(new Date());
        } else if (TimeDimensionEnum.YEAR.getCode().equals(maintainTopRequest.getDateType())) {
            count = 12;
        }
        return count;
    }


    /**
     * 循环，没有的set 0
     *
     * @param
     * @param count
     */
    private Map loopCount(Map reqMap, int count) {
        Map resMap = new LinkedHashMap();
        for (int i = 1; i <= count; i++) {
            String key = String.valueOf(i).length() < 2 ? "0" + i : i + "";
            if (reqMap.get(key) == null) {
                resMap.put(key, 0);
            } else {
                resMap.put(key, reqMap.get(key));
            }
        }
        return resMap;
    }

    /**
     * 图形化数据
     *
     * @param maintainTopRequest
     * @return
     */
    public Map maintainGraphInfo(MaintainTopRequest maintainTopRequest) {
        Function<MaintainTopRequest, Map> function = maintainGraphInfoType.get(maintainTopRequest.getQueryType());
        if (function == null) {
            throw new BaseException("queryType 参数有问题");
        }
        return function.apply(maintainTopRequest);
    }


}
